// 方式二，while比for更快
function longestPalindrome(s) {
  if (s.length < 2) return s;
  let ret = "";
  let n = 0;
  while (n < s.length) {
    let m = n + 1;
    while (m < s.length + 1) {
      const temp = s.substring(n, m);
      const palindrome = isPalindrome(temp);
      if (palindrome && ret.length < temp.length) {
        ret = temp;
      }
      m++;
    }
    n++;
  }
  return ret;
  function isPalindrome(x) {
    for (let i = 0, len = x.length; i < len / 2; i++) {
      if (x[i] != x[len - i - 1]) return false;
    }
    return true;
  }
}

const palindrome = longestPalindrome("sssss");
console.log(palindrome);
